﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using 控件调整.Common;

namespace 控件调整.Components
{
    /// <summary>
    /// FileUpload.xaml 的交互逻辑
    /// </summary>
    public partial class FileUpload : UserControl
    {
        public FileUpload()
        {
            InitializeComponent();
        }

        // 将 ProcessUpload 修改为依赖属性
        public Func<List<FileInfo>, Response> ProcessUpload
        {
            get { return (Func<List<FileInfo>, Response>)GetValue(ProcessUploadProperty); }
            set { SetValue(ProcessUploadProperty, value); }
        }

        public static readonly DependencyProperty ProcessUploadProperty =
            DependencyProperty.Register("ProcessUpload", typeof(Func<List<FileInfo>, Response>), typeof(FileUpload));

        // 将 SuccessHandle 修改为依赖属性
        public Action<Response> SuccessHandle
        {
            get { return (Action<Response>)GetValue(SuccessHandleProperty); }
            set { SetValue(SuccessHandleProperty, value); }
        }

        public static readonly DependencyProperty SuccessHandleProperty =
            DependencyProperty.Register("SuccessHandle", typeof(Action<Response>), typeof(FileUpload));

        // 将 FailHandle 修改为依赖属性
        public Action<Response> FailHandle
        {
            get { return (Action<Response>)GetValue(FailHandleProperty); }
            set { SetValue(FailHandleProperty, value); }
        }

        public static readonly DependencyProperty FailHandleProperty =
            DependencyProperty.Register("FailHandle", typeof(Action<Response>), typeof(FileUpload));

        public List<FileInfo> FileList
        {
            get { return (List<FileInfo>)GetValue(FileListProperty); }
            set { SetValue(FileListProperty, value); }
        }

        public static readonly DependencyProperty FileListProperty =
            DependencyProperty.Register("FileList", typeof(List<FileInfo>), typeof(FileUpload ), new PropertyMetadata(new List<FileInfo>(),OnPropertyChanged));


        public bool AllowMulti
        {
            get { return (bool)GetValue(AllowMultiProperty); }
            set { SetValue(AllowMultiProperty, value); }
        }

        public static readonly DependencyProperty AllowMultiProperty =
            DependencyProperty.Register("AllowMulti", typeof(bool), typeof(FileUpload), new PropertyMetadata(false,OnPropertyChanged));


        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as FileUpload).Refresh();
        }

        private void Refresh()
        {

        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "图片文件|*.jpg;*.png;*.gif";
            openFileDialog.Title = "请选择要上传的头像";
            openFileDialog.Multiselect = AllowMulti;

            bool? flag = openFileDialog.ShowDialog();
            if (flag != null && flag == false) return;

            string[] fileNames = openFileDialog.FileNames;
            foreach (string fileName in fileNames)
            {
                if (File.Exists(fileName))
                {
                    FileList.Add(new FileInfo(fileName));
                }
            }

            // 处理文件上传
            Response? response = ProcessUpload?.Invoke(FileList);
            if (response == null) return;

            if(response.StatusCode == 200)
            {
                SuccessHandle?.Invoke(response);
            }
            else
            {
                FailHandle?.Invoke(response);
            }
        }



        private string GetFileName(string path)
        {
            char separator = System.IO.Path.DirectorySeparatorChar;
            int idx2 = path.LastIndexOf(separator) + 1;
            return path.Substring(idx2);
        }
    }
}
